package ch.upndown.upndown.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

/**
 * Created by Bernard on 02.08.2014.
 * Michel added table CheckHistory on 03.08.2014
 */
public final class DatabaseCreator extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "UPnDOWN.db";
    public static final int DATABASE_VERSION = 2;

    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + Websites.TABLE_NAME + " (" +
                    Websites.COLUMN_NAME_ENTRY_ID + " INTEGER PRIMARY KEY," +
                    Websites.COLUMN_NAME_TITLE + " TEXT," +
                    Websites.COLUMN_NAME_URL + " TEXT);" +

                    "CREATE TABLE " + WebsitesChecks.TABLE_NAME + " (" +
                    WebsitesChecks.COLUMN_NAME_ENTRY_WEBSITE_ID + " INTEGER," +
                    WebsitesChecks.COLUMN_NAME_ENTRY_CHECKS_ID + " INTEGER," +
                    WebsitesChecks.COLUMN_NAME_ACTIVE + " INTEGER," +
                    WebsitesChecks.COLUMN_NAME_INTERVAL + " INTEGER, " +
                    "PRIMARY KEY (" + WebsitesChecks.COLUMN_NAME_ENTRY_WEBSITE_ID + WebsitesChecks.COLUMN_NAME_ENTRY_CHECKS_ID + ")" +
                    "FOREIGN KEY (" + WebsitesChecks.COLUMN_NAME_ENTRY_WEBSITE_ID + ") REFERENCES " + Websites.TABLE_NAME + "(" + Websites.COLUMN_NAME_ENTRY_ID + ") ON DELETE CASCADE," +
                    "FOREIGN KEY (" + WebsitesChecks.COLUMN_NAME_ENTRY_CHECKS_ID + ") REFERENCES " + Checks.TABLE_NAME + "(" + Checks.COLUMN_NAME_ENTRY_ID + ") ON DELETE CASCADE);" +

                    "CREATE TABLE " + Checks.TABLE_NAME + " (" +
                    Checks.COLUMN_NAME_ENTRY_ID + " INTEGER PRIMARY KEY," +
                    Checks.COLUMN_NAME_TITLE + " TEXT);" +

                    "CREATE TABLE " + CheckHistory.TABLE_NAME + " (" +
                    CheckHistory.COLUMN_NAME_ENTRY_WEBSITE_ID + " INTEGER," +
                    CheckHistory.COLUMN_NAME_ENTRY_CHECKS_ID + " INTEGER," +
                    CheckHistory.COLUMN_NAME_TIMESTAMP + " DATETIME," +
                    CheckHistory.COLUMN_NAME_EXIT_CODE + " INTEGER, " +
                    "PRIMARY KEY (" + CheckHistory.COLUMN_NAME_ENTRY_WEBSITE_ID + CheckHistory.COLUMN_NAME_ENTRY_CHECKS_ID + CheckHistory.COLUMN_NAME_TIMESTAMP + ")" +
                    "FOREIGN KEY (" + CheckHistory.COLUMN_NAME_ENTRY_WEBSITE_ID + ") REFERENCES " + Websites.TABLE_NAME + "(" + Websites.COLUMN_NAME_ENTRY_ID + ") ON DELETE CASCADE," +
                    "FOREIGN KEY (" + CheckHistory.COLUMN_NAME_ENTRY_CHECKS_ID + ") REFERENCES " + Checks.TABLE_NAME + "(" + Checks.COLUMN_NAME_ENTRY_ID + ") ON DELETE CASCADE);";

    private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + Websites.TABLE_NAME + ";" +
            "DROP TABLE IF EXISTS " + WebsitesChecks.TABLE_NAME + ";" +
            "DROP TABLE IF EXISTS " + Checks.TABLE_NAME + ";" +
            "DROP TABLE IF EXISTS " + CheckHistory.TABLE_NAME + ";";

    public DatabaseCreator(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }


    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

    public static abstract class Websites implements BaseColumns {
        public static final String TABLE_NAME = "websites";
        public static final String COLUMN_NAME_ENTRY_ID = "id";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_URL = "url";
    }

    public static abstract class WebsitesChecks implements BaseColumns {
        public static final String TABLE_NAME = "websites_ckecks";
        public static final String COLUMN_NAME_ENTRY_WEBSITE_ID = "website_id";
        public static final String COLUMN_NAME_ENTRY_CHECKS_ID = "checks_id";
        public static final String COLUMN_NAME_ACTIVE = "active";
        public static final String COLUMN_NAME_INTERVAL = "interval";
    }

    public static abstract class Checks implements BaseColumns {
        public static final String TABLE_NAME = "checks";
        public static final String COLUMN_NAME_ENTRY_ID = "id";
        public static final String COLUMN_NAME_TITLE = "name";
    }

    public static abstract class CheckHistory implements BaseColumns {
        public static final String TABLE_NAME = "check_history";
        public static final String COLUMN_NAME_ENTRY_WEBSITE_ID = "website_id";
        public static final String COLUMN_NAME_ENTRY_CHECKS_ID = "checks_id";
        public static final String COLUMN_NAME_TIMESTAMP = "timestamp";
        public static final String COLUMN_NAME_EXIT_CODE = "exit_code";
        /** Exit Codes Vorschlag:
         *      0xx - Successful
         *      1xx - Information
         *      2xx - Failed
         *      */
    }
}
